import {routerRedux} from 'dva/router';
import api from '../api';
import {message} from 'antd'
import Mock from 'mockjs';
export default {
  namespace : 'dailyReplay',
  state : {
    currentpage:1,//当前页
    pagesize:15,//当前页数据条数
    sortField:'',//排序字段
    sortOrder:'',//排序类型
    firstdate: null, // 最早时间 默认值：null YYYY-MM-DD
    lastdate: null, // 最后时间 默认值：null YYYY-MM-DD
    begindate: null, // 开始时间 默认值：null YYYY-MM-DD
    enddate: null, // 结束时间 默认值：null YYYY-MM-DD
    trader: 'total', // 交易员ID 默认值：团队
    traderDefinitionList:[],// 交易员定义列表 默认值： []
    stock: 'total', // 股票ID 默认值：所有股票
    stockDefinitionList:[],// 股票定义列表 默认值：[]
    traderQuota:{},// 指标展示数据 默认值：{}
    traderIndex: {}, // 交易员指标数据 默认值：{}
    stockIndex: {}, // 股票指标数据 默认值：{}
    stockYield: [], // 股票收益数据 默认值：[]
    tradingYieldDistribution: [], // 交易收益率分布数据 默认值：[]
    tradingTimeDistribution: [], // 交易时段分布数据 默认值：[]
    stockQuota:{},// 股票展示数据 默认值：{}
    tradeRecords:{},//交易记录数据 默认值：{}
    data:{},//股票表现数据 默认值：{}
    cpGroupId:null,//该笔交易ID
    orderDetails:[], //交易情况 默认值:[]
    openPositionsList:{},//未平仓详情列表 默认值：{}
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        // 没有参数时，向后端请求数据，并且改变query
        if (pathname === '/tradeReplay/dailyReplay') {
          dispatch({type: 'init', payload: query})
        }
      })
    }
  },
  effects : {
    * init({
				payload
			}, {call, put,select}) {
        // 初始化
				const state=yield select(state => state.dailyReplay)
				// 看本次的参数和上次的参数是否一致：
				// 保存参数到state
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(!Object.getOwnPropertyNames(payload).length||JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
						type:'setState',
						payload
					})
				}
		},
    * getTraderStockDefinitionList({
      payload
    }, {call, put, select}) {
      // 获取交易员定义列表数据:begindate | enddate
      const query={}
      payload.begindate&&(query.begindate=payload.begindate)
      payload.enddate&&(query.enddate=payload.enddate)

      const res = yield call(api.replay.getTSDefinitionList, query)
      if (res.code === 0) {
        // const traderDefinitionList = res.data.list
        const stockDefinitionList = res.data.symbolList;
        const traderDefinitionList = res.data.traderList
        const date=res.data.date
        yield put({type: 'setState', payload: {
            traderDefinitionList,
            stockDefinitionList,
            ...date
          }})
      } else {
        message.info('获取交易员及股票定义列表数据失败！')
      }
    },
    * getStockDefinitionList({
      payload
    }, {call, put, select}) {
      // 获取股票定义列表数据:begindate | enddate
      const query={}
      payload.begindate&&(query.begindate=payload.begindate)
      payload.enddate&&(query.enddate=payload.enddate)

      const res = yield call(api.replay.getTSDefinitionList, query)
      if (res.code === 0) {
        const stockDefinitionList = res.data.list
        yield put({type: 'setState', payload: {
            stockDefinitionList,
          }})
      } else {
        message.info('获取股票定义列表数据失败！')
      }
    },
    * getTraderQuota({
      payload
    }, {call, put, select}) {
      // 获取交易员指标数据:trader | begindate | enddate
      const query = {}
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getTraderIndex, query)
      if (res.code === 0) {
        const traderQuota = res.data
        yield put({type: 'setState', payload: {
            traderQuota
          }})
      } else {
        message.info('获取交易员指标数据失败！')
      }
    },
    * getTraderIndexList({
      payload
    }, {call, put, select}) {
      // 获取交易员指标列表数据
      const query = {}
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getTraderIndex, query)
      if (res.code === 0) {
        const traderIndex = res.data
        yield put({type: 'setState', payload: {
            traderIndex
          }})
      } else {
        message.info('获取交易员指标列表数据失败！')
      }
    },
    * getStockIndexList({
      payload
    }, {call, put, select}) {
      // 获取股票指标数据
      const query = {}
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getStockIndex, query)
      if (res.code === 0) {
        const stockIndex = res.data
        yield put({type: 'setState', payload: {
            stockIndex,
          }})
      } else {
        message.info('获取股票指标数据失败！')
      }
    },
    * getStockYield({
      payload
    }, {call, put, select}) {
      // 获取股票收益数据
      const query = {}
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getStockYield, query)
      if (res.code === 0) {
        const stockYield = res.data
        yield put({type: 'setState', payload: {
            stockYield
          }})
      } else {
        message.info('获取股票收益数据失败！')
      }
    },
    * getTradingYieldDistribution({
      payload
    }, {call, put, select}) {
      // 获取交易收益率分布数据
      const query = {}
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getTradingYieldDistribution, query)
      if (res.code === 0) {
        const tradingYieldDistribution = res.data
        yield put({type: 'setState', payload: {
            tradingYieldDistribution
          }})
      } else {
        message.info('获取交易收益率分布数据失败！')
      }
    },
    * getTradingTimeDistribution({
      payload
    }, {call, put}) {
      // 获取交易时段分布数据
      const query = {}
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)

      const res = yield call(api.replay.getTradingTimeDistribution, query)
      if (res.code === 0) {
        const tradingTimeDistribution = res.data
        yield put({type: 'setState', payload: {
            tradingTimeDistribution
          }})
      } else {
        message.info('获取交易时段分布数据失败！')
      }
    },
    * getStockQuota({
      payload
    }, {call, put, select}) {
      // 获取股票指标数据:symbol | begindate | enddate
      const query = {}
      console.log('getStockQuota>payload',payload)
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.stock  && (query.symbol = payload.stock)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)
      console.log('getStockQuota>query',query)
      const res = yield call(api.replay.getStockIndex, query)
      if (res.code === 0) {
        const stockQuota = res.data
        console.log('getStockQuota>',stockQuota)
        yield put({type: 'setState', payload: {
            stockQuota
          }})
      } else {
        message.info('获取股票指标数据失败！')
      }
    },
    * getTradeRecords({
      payload
    }, {call, put, select}) {
      // 获取交易记录列表数据
      const query = {}
      console.log('getTradeRecords>payload',payload)
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.stock && (payload.stock != 'total') && (query.symbol = payload.stock)
      payload.currentpage && (query.currentpage = payload.currentpage)
      payload.pagesize && (query.pagesize = payload.pagesize)
      payload.sortField && (query.sortField = payload.sortField)
      payload.sortOrder && (query.sortOrder = payload.sortOrder)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)
      console.log('getTradeRecords>query',query)
      const res = yield call(api.replay.getTradeRecords, query)
      if (res.code === 0) {
        const tradeRecords = res.data
        yield put({type: 'setState', payload: {
            tradeRecords
          }})
      } else {_
        message.info('获取交易记录列表数据失败！')
      }
    },
    *getStockPerformance({
      payload
    },{call,put,select}){
      // 获取股票表现数据
      const query = {}
      console.log('getStockPerformance',payload)
      payload.trader && (payload.trader != 'total') && (query.trader = payload.trader)
      payload.stock && (payload.stock != 'total') && (query.symbol = payload.stock)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)
      // payload.cpGroupId && (query.cpGroupId = payload.cpGroupId)
      // payload.dailyReplay && (query.begindate = payload.dailyReplay.begindate)
      // payload.dailyReplay && (query.enddate = payload.dailyReplay.enddate)
      const res = yield call(api.replay.getStockPerformance, query)
      if (res.code === 0) {
        const data = res.data
        yield put({type: 'setState', payload: {
            data
          }})
      } else {_
        message.info('获取股票表现数据失败！')
      }
    },
    *getOrderDetails({
      payload
    },{call,put,select}){
      const state=yield select(state => state.dailyReplay);
      console.log('getOrderDetails',payload,state)
      const oIquery = {};//获取订单信息数据
      const sPquery = {};//获取股票表现数据
      const oLquery = {};//获取订单列表数据
      payload.trader && (payload.trader != 'total') && (oIquery.trader = sPquery.trader = oLquery.trader= payload.trader)
      payload.symbol && (payload.stock != 'total') && (oIquery.symbol = sPquery.symbol = oLquery.symbol = payload.symbol)
      payload.firstTradeDate && (oIquery.begindate = sPquery.begindate = oLquery.begindate = payload.firstTradeDate)
      payload.lastTradeDate && (oIquery.enddate = sPquery.enddate = oLquery.enddate = payload.lastTradeDate)
      payload.cpGroupId && (oIquery.cpgroupid = sPquery.cpgroupid = oLquery.cpgroupid = payload.cpGroupId)
      console.log('getOrderDetails>>>>',payload,'oIquery>',oIquery,'sPquery>',sPquery,'oLquery>',oLquery)
      
      const oIres = yield call(api.replay.getOrderInfo, oIquery)
      const oLres = yield call(api.replay.getOrderList, oLquery)
      const sPres = yield call(api.replay.getStockPerformance2, sPquery)
      const orderDetails1 = {}
      orderDetails1.cpGroupId = payload.cpGroupId;
      orderDetails1.oIres = oIres.data;
      orderDetails1.oLres = oLres.data;
      orderDetails1.sPres = sPres.data;
      const a =[]
      a.push(payload.cpGroupId)
      state.tradeRecords.list.map((item,i)=>{
        if(item.cpGroupId == orderDetails1.cpGroupId){
          item.orderDetails = orderDetails1
        }
      })
      state.orderDetails.push(orderDetails1)
      state.cpGroupId = payload.cpGroupId;
      console.log('getOrderDetails>',state.orderDetails,)
      yield put({type: 'setState', payload: {
        'orderDetails':state.orderDetails,
        'cpGroupId':a
      }})
    },
    *getTSDefinitionList({
      payload
    },{call,put,select}){
      const query = {}
      console.log('11313',payload)
      payload.query.trader && (payload.query.trader != 'total') && (query.trader= payload.query.trader)
      payload.query.stock&& (payload.query.stock != 'total') && (query.symbol = payload.query.stock)
      payload.query.begindate && (query.begindate = payload.query.begindate)
      payload.query.enddate && (query.enddate = payload.query.enddate)
      
      const res = yield call(api.replay.getTSDefinitionList, query)
      
      if (res.code === 0) {
        const stockDefinitionList = res.data.symbolList;
        const traderDefinitionList = res.data.traderList
        yield put({type: 'setState', payload: {
          stockDefinitionList,
          traderDefinitionList
        }})
      } else {
        message.info('获取列表数据失败！')
      }
    },
    *getOpenPositionsList({
      payload
    },{call,put,select}){
      const query = {}
      console.log('11313',payload)
      payload.trader && (payload.trader != 'total') && (query.trader= payload.trader)
      payload.stock&& (payload.stock != 'total') && (query.symbol = payload.stock)
      payload.begindate && (query.begindate = payload.begindate)
      payload.enddate && (query.enddate = payload.enddate)
      payload.currentpage && (query.currentpage = payload.currentpage)
      payload.pagesize && (query.pagesize = payload.pagesize)
      const res = yield call(api.replay.getOpenPositionsList, query)
      console.log('sadsadsa',res)
      if (res.code === 0) {
        const openPositionsList = res.data;
        yield put({type: 'setState', payload: {
          openPositionsList,
        }})
      } else {
        message.info('获取列表数据失败！')
      }
    }
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}
